﻿# -*- coding:utf-8 -*-
#
# Индексная страница
#

from django.db import models
from index.utils import thumbnail

class Section(models.Model):
    title = models.CharField(max_length=128, verbose_name="Название")
    short_title = models.CharField(max_length=48, verbose_name="Сокращенное название") 
    description = models.TextField(max_length=512, blank=True, verbose_name="Описание") 
    url = models.CharField(max_length=64, verbose_name="Адрес") 
    sort_order = models.IntegerField(default=10, verbose_name="Сортировка")
    picture = models.ImageField(upload_to='files/index/section/img/', verbose_name="Изображение")
    visible = models.BooleanField(verbose_name="Видимость")

    class Meta:
        ordering = ['sort_order']
        
    def __unicode__(self):
        return u"%s" % (self.title) 

    def preview_image_url(self):
        if not self.picture:
            return 'Нет изображения'
        image_path = thumbnail(self.picture, '50x50')
        return '<a href="'+ str(self.id) +'/"><img src="' + str(image_path) +'"/></a>'

    preview_image_url.short_description = 'Картинка'
    preview_image_url.allow_tags = True
